package com.yzh.wanandroid.component.fragment

import android.view.View
import android.widget.Toast
import com.yzh.core.navigation.NavHostFragment
import com.yzh.wanandroid.R
import com.yzh.wanandroid.base.MvvmFragment
import com.yzh.wanandroid.component.activity.MainActivity
import com.yzh.wanandroid.databinding.FragmentRegisterBinding
import com.yzh.wanandroid.model.RegistInfoData
import com.yzh.wanandroid.utils.ERROR
import com.yzh.wanandroid.utils.LOADING
import com.yzh.wanandroid.utils.SUCCESS
import com.yzh.wanandroid.view.LoadingDialog
import com.yzh.wanandroid.vm.RegisterFragmentVm

class RegisterFragment : MvvmFragment<FragmentRegisterBinding, RegisterFragmentVm>() {

    override fun initViewModel(): RegisterFragmentVm = createViewModel()

    override fun getLayoutId() = R.layout.fragment_register

    private val mLoadingStateDialog: LoadingDialog by lazy { LoadingDialog(requireContext()) }

    override fun initView(view: View) {
        initTitle()
    }

    private fun initTitle() {
        mBinding.run {
            registerTitle.titleText.setText(R.string.user_register)
            registerTitle.toolbar.navigationIcon =
                requireActivity().getDrawable(R.drawable.arrow_back)
            registerTitle.toolbar.setNavigationOnClickListener {
                requireActivity().onBackPressed()
            }
        }
    }

    override fun initData() {
        initBinding()
        onRegisteState()
        onLoginState()
    }

    private fun initBinding() {
        mBinding.fragment = this
    }

    private fun onRegisteState() {
        mViewModel.mRegister.observe(this) {
            when (it.loadingState) {
                LOADING -> mLoadingStateDialog.show()
                SUCCESS -> it.data?.let { registInfo -> onRegisterSuccess(registInfo) }
                ERROR -> onRegisterFail(it.message)
            }
        }
    }

    private fun onLoginState() {
        mViewModel.mLoginData.observe(this) {
            when (it?.loadingState) {
                SUCCESS -> onLoginSuccess()
                ERROR -> onLoginFail(it.message)
            }
        }
    }

    fun onRegisterClick() {
        mBinding.run {
            mViewModel.userRegister(
                this@RegisterFragment,
                etUsername.text.toString(),
                etPassword.text.toString(),
                etPassword2.text.toString()
            )
        }
    }

    fun toLogin() {
        NavHostFragment.findNavController(this).navigateUp()
    }

    private fun onRegisterSuccess(registInfoData: RegistInfoData) {
        Toast.makeText(activity, R.string.register_success, Toast.LENGTH_SHORT).show()
        mLoadingStateDialog.cancel()
        mViewModel.loginWanAndroid(this,registInfoData.userName,registInfoData.passWord)
    }

    private fun onRegisterFail(message: String) {
        mLoadingStateDialog.cancel()
        Toast.makeText(activity, message, Toast.LENGTH_SHORT).show()
    }

    private fun onLoginSuccess() {
        val host = (requireActivity() as MainActivity).getHostFragment()
        for (fragment in host.childFragmentManager.fragments) {
            if (fragment is LoginFragment) {
                NavHostFragment.findNavController(fragment).navigateUp()
            }
        }
        NavHostFragment.findNavController(this).navigateUp()
        Toast.makeText(activity, R.string.login_success, Toast.LENGTH_SHORT).show()
    }

    private fun onLoginFail(errorMessage: String) {
        Toast.makeText(activity, errorMessage, Toast.LENGTH_SHORT).show()
    }
}